package com.moive.news.tqnewsapplication.ui.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.google.gson.Gson;
import com.moive.news.tqnewsapplication.R;
import com.moive.news.tqnewsapplication.base.BaseActivity;
import com.moive.news.tqnewsapplication.event.SenEvent;
import com.moive.news.tqnewsapplication.networks.BaseApi;
import com.moive.news.tqnewsapplication.okmanager.OkManagerCallback;
import com.moive.news.tqnewsapplication.utils.LogUtil;
import com.moive.news.tqnewsapplication.utils.PreferenceUtil;
import com.moive.news.tqnewsapplication.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;
import butterknife.ButterKnife;
import okhttp3.Call;
/**
 * Created by Administrator on 2017/11/9.
 * 注册
 */
public class RegistActivity extends BaseActivity implements View.OnClickListener {
    private EditText reg_usernichen;
    private EditText reg_userpwd;
    private EditText reg_userpwd02;
    private EditText reg_phone;
    private Button btn_regist;
    private ImageView regist_back;
    String phone = "", nichen = "", userpwd = "", userpwd02 = "", code = "", getcode = "";
    private static final String TAG = "RegistActivity";
  private Gson gson=new Gson();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        ButterKnife.bind(this);
        initViews();
    }
    public void initViews() {
        reg_usernichen = (EditText) findViewById(R.id.reg_usernichen);
        reg_userpwd = (EditText) findViewById(R.id.reg_userpwd);
        reg_userpwd02 = (EditText) findViewById(R.id.reg_userpwd02);
        reg_phone = (EditText) findViewById(R.id.reg_phone);
        btn_regist = (Button) findViewById(R.id.btn_regist);
        regist_back= (ImageView) findViewById(R.id.regist_back);
        btn_regist.setOnClickListener(this);
        regist_back.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_regist:
                regist();
                break;
            case R.id.regist_back:
                finish();
                break;
        }
    }
    private void regist() {
        nichen = reg_usernichen.getText().toString().trim();
        userpwd = reg_userpwd.getText().toString().trim();
        userpwd02 = reg_userpwd02.getText().toString().trim();
        phone = reg_phone.getText().toString().trim();
        if (TextUtils.isEmpty(nichen)) {
            ToastUtils.ShortToast("请输入昵称！");
            return;
        }
        if (TextUtils.isEmpty(userpwd)) {
            ToastUtils.ShortToast("请输入密码!");
            return;
        }
        if (TextUtils.isEmpty(userpwd02)) {
            ToastUtils.ShortToast("请重复输入密码！");
            return;
        }
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.ShortToast("请输入电话号码！");
        }

        if (!userpwd.equals(userpwd02)) {
            ToastUtils.ShortToast("2次输入密码不一致，请重新检查密码！");
            return;
        }

        //注册接口
        OkHttpUtils.post().
                url(BaseApi.RIGEST)
                .addParams("nickname",nichen)
                .addParams("telephone",phone)
                .addParams("password",userpwd)
                .build().execute(new OkManagerCallback(this) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
            }
            @Override
            public void onResponse(String response, int id) {
                super.onResponse(response, id);
                try {
                    JSONObject jsonObject=new JSONObject(response);
                    LogUtil.i(TAG,jsonObject.toString());
                    if (jsonObject.optInt("code")==0){
                        String data = jsonObject.getString("data");
                        PreferenceUtil.put("token", data);
                        startActivity(new Intent(RegistActivity.this, MainActivity.class));
                        EventBus.getDefault().post(new SenEvent("2"));
                        ToastUtils.ShortToast("注册成功！");
                    }else {
                        ToastUtils.ShortToast(jsonObject.getString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
